package com.twh.www.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twh.www.entity.WnUser;
import com.twh.www.service.MinioService;
import com.twh.www.service.WnUserService;
import com.twh.www.mapper.WnUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;


@Service
public class WnUserServiceImpl extends ServiceImpl<WnUserMapper, WnUser>
    implements WnUserService{
    @Autowired
    private MinioService minioService;
    @Autowired
    private WnUserMapper wnUserMapper;


    @Override
    public WnUser getUserByAccount(String account) {
        WnUser user = wnUserMapper.getUserByAccount(account);
        System.out.println("Account: " + account + ", User: " + user);
        return user;
    }


    @Override
    public boolean insertUser(String account, String password, String telphone, String email, MultipartFile avatar) throws IOException {
        if (getUserByAccount(account) != null) {
            return false;
        }
        String avatarPath = saveAvatar(avatar);
        WnUser newUser = new WnUser();
        newUser.setAccount(account);
        newUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        newUser.setTelphone(telphone);
        newUser.setEmail(email);
        newUser.setAvatar(avatarPath);
        newUser.setPoints(0);
        newUser.setMoney(BigDecimal.valueOf(0.00));
        newUser.setRegTime(LocalDateTime.now());
        newUser.setStatus("1");
        return baseMapper.insert(newUser) > 0;
    }

    @Override
    public String saveAvatar(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return "/static/upload/avatar/6496fc47-129d-4501-a1dd-d6f58209a019.jpg";
        }
        try {
            // 使用 MinioService 上传文件
            String avatarUrl = minioService.uploadFile(file);
            return avatarUrl; // 返回 MinIO 上的文件访问 URL
        } catch (Exception e) {
            e.printStackTrace();
            throw new IOException("上传头像失败: " + e.getMessage());
        }
    }

    @Override
    public boolean updateUserInfo(WnUser user) {
        return baseMapper.updateById(user) > 0;
    }

//    @Override
//    public WnUser getUserByPhone(String phone) {
//        return wnUserMapper.getUserByPhone(phone);
//    }
//
//    @Override
//    public WnUser getUserByEmail(String email) {
//        return wnUserMapper.getUserByEmail(email);
//    }

    @Override
    public boolean freezeUser(Integer id) {
        return wnUserMapper.updateUserStatus(id, "2") > 0;
    }

    @Override
    public boolean unfreezeUser(Integer id) {
        return wnUserMapper.updateUserStatus(id, "0") > 0;
    }

    @Override
    public boolean addToBlacklist(Integer id) {
        return wnUserMapper.updateUserStatus(id, "3") > 0;
    }

    @Override
    public boolean removeFromBlacklist(Integer id) {
        return wnUserMapper.updateUserStatus(id, "0") > 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return wnUserMapper.updateUserStatus(id, "4") > 0;
    }

    public boolean logout(Integer id){
        return wnUserMapper.deleteUser(id) > 0;
    }
}




